﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Collections.ObjectModel;
using System;
using MyData.ExpressionBuilder.Interfaces;
using MyData.ExpressionBuilder.Model;
using System.Windows.Forms;
using System.Windows.Controls;


namespace MyData.ExpressionBuilder.BaseExtensions
{
  public abstract class ExpressionBuilderBase : IExpressionExtension, IExpressionBuilder, INotifyPropertyChanged
  {
    protected abstract IEnumerable<ListItem> RunExtension(IEnumerable<ListItem> metadata);

    public event PropertyChangedEventHandler PropertyChanged;
    public Action<object> OnUpdated { get; set; }

    protected ExpressionBuilderBase() { }

    private IExpressionItemsProvider _itemsProvider;
    public IExpressionItemsProvider ItemsProvider
    {
      get { return this._itemsProvider; }
      set
      {
        this._itemsProvider = value;
        this.NotifyPropertyChanged("ItemsProvider");
      }
    }


    private IExpressionTypeConverter _expressionTypeConverter;
    public IExpressionTypeConverter Converter
    {
      get { return this._expressionTypeConverter; }
      set
      {
        this._expressionTypeConverter = value;
        this.NotifyPropertyChanged("Converter");
      }
    }

    private List<ListItem> _results;
    public List<ListItem> Results
    {
      get { return this._results; }
      set { this._results = value; this.ResultsCount = this._results.Count(); 
        this.NotifyPropertyChanged("Results"); }
    }

    private int _resultsCount;
    public int ResultsCount
    {
      get { return this._resultsCount; }
      set { this._resultsCount = value; this.NotifyPropertyChanged("ResultsCount"); }
    }

    //ViewModel
    private void NotifyPropertyChanged(string name)
    {
      if (this.PropertyChanged != null)
      {
        this.PropertyChanged(this, new PropertyChangedEventArgs(name));
      }
    }

    public void Run(IEnumerable<ListItem> metadata)
    {
      this.RunExtension(metadata);  
    }

    private ItemsControl _itemsControl = null;
    public ItemsControl ItemsControl
    {
      get { return this._itemsControl; }
      set
      {
        this._itemsControl = value;
        this.NotifyPropertyChanged("ItemsControl");
      }
    }
  }
}

